package cn.gp.chatclient.ui.component;

import cn.gp.chatclient.model.UserVo;
import cn.gp.chatclient.utils.ImageUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.RenderingHints;
import java.awt.BasicStroke;

/**
 * @ClassName ContactListCellRenderer
 * @Description TODO
 * @Author He Guoping
 * @Date 2025/6/17 9:26
 * @Version JDK17
 */
public class ContactListCellRenderer extends DefaultListCellRenderer {
    private static final Color ONLINE_COLOR = new Color(0, 180, 0); // 在线状态绿色
    private static final Color OFFLINE_COLOR = new Color(150, 150, 150); // 离线状态灰色
    private static final Color SELECTED_BG = new Color(240, 240, 240); // 选中背景色
    private static final Color DEFAULT_BG = Color.WHITE; // 默认背景色
    private static final int AVATAR_SIZE = 40; // 头像大小

    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value, int index,
                                                  boolean isSelected, boolean cellHasFocus) {
        JPanel panel = new JPanel(new BorderLayout(10, 0));
        panel.setBackground(isSelected ? SELECTED_BG : DEFAULT_BG);
        panel.setBorder(BorderFactory.createEmptyBorder(8, 12, 8, 12));

        if (value instanceof UserVo.FriendVo) {
            UserVo.FriendVo friend = (UserVo.FriendVo) value;

            // 创建头像面板
            JPanel avatarPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            avatarPanel.setBackground(panel.getBackground());
            JLabel avatarLabel = new JLabel();
            avatarLabel.setPreferredSize(new Dimension(AVATAR_SIZE, AVATAR_SIZE));
            
            // 设置头像
            if (friend.getAvatar() != null && !friend.getAvatar().isEmpty()) {
                avatarLabel.setIcon(ImageUtil.getScaledImageIcon(friend.getAvatar(), AVATAR_SIZE, AVATAR_SIZE));
            } else {
                // 创建默认头像
                avatarLabel.setIcon(createDefaultAvatar());
            }
            avatarPanel.add(avatarLabel);

            // 创建信息面板
            JPanel infoPanel = new JPanel(new BorderLayout(5, 5));
            infoPanel.setBackground(panel.getBackground());

            // 用户名标签
            JLabel usernameLabel = new JLabel(friend.getUsername());
            usernameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
            infoPanel.add(usernameLabel, BorderLayout.CENTER);

            // 在线状态指示器
            JPanel statusPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            statusPanel.setBackground(panel.getBackground());
            JLabel statusLabel = new JLabel("●");
            statusLabel.setFont(new Font("微软雅黑", Font.BOLD, 12));
            statusLabel.setForeground(friend.getStatus() != null && friend.getStatus() == 0 ? ONLINE_COLOR : OFFLINE_COLOR);
            statusPanel.add(statusLabel);

            // 添加组件到面板
            panel.add(avatarPanel, BorderLayout.WEST);
            panel.add(infoPanel, BorderLayout.CENTER);
            panel.add(statusPanel, BorderLayout.EAST);

            // 添加鼠标悬停效果
            panel.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    if (!isSelected) {
                        panel.setBackground(new Color(245, 245, 245));
                        avatarPanel.setBackground(panel.getBackground());
                        infoPanel.setBackground(panel.getBackground());
                        statusPanel.setBackground(panel.getBackground());
                    }
                }

                public void mouseExited(java.awt.event.MouseEvent evt) {
                    if (!isSelected) {
                        panel.setBackground(DEFAULT_BG);
                        avatarPanel.setBackground(panel.getBackground());
                        infoPanel.setBackground(panel.getBackground());
                        statusPanel.setBackground(panel.getBackground());
                    }
                }
            });
        }

        return panel;
    }

    private ImageIcon createDefaultAvatar() {
        // 创建一个简单的默认头像
        BufferedImage image = new BufferedImage(AVATAR_SIZE, AVATAR_SIZE, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();
        
        // 设置抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        // 绘制圆形背景
        g2d.setColor(new Color(200, 200, 200));
        g2d.fillOval(0, 0, AVATAR_SIZE, AVATAR_SIZE);
        
        // 绘制边框
        g2d.setColor(new Color(150, 150, 150));
        g2d.setStroke(new BasicStroke(2));
        g2d.drawOval(1, 1, AVATAR_SIZE-3, AVATAR_SIZE-3);
        
        // 绘制用户图标
        g2d.setColor(new Color(100, 100, 100));
        g2d.fillOval(AVATAR_SIZE/4, AVATAR_SIZE/4, AVATAR_SIZE/2, AVATAR_SIZE/2);
        
        g2d.dispose();
        return new ImageIcon(image);
    }
}
